<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello Controller Test Page</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background-color: #fafafa;
        }
        .test-section h2 {
            color: #555;
            margin-top: 0;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
            font-size: 14px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 15px;
            padding: 15px;
            border-radius: 5px;
            min-height: 20px;
            font-family: monospace;
            white-space: pre-wrap;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .loading {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
        }
        input[type="text"] {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
            width: 200px;
        }
        .input-group {
            margin-bottom: 10px;
        }
        .status {
            text-align: center;
            padding: 10px;
            margin-bottom: 20px;
            border-radius: 5px;
            font-weight: bold;
        }
        .status.online {
            background-color: #d4edda;
            color: #155724;
        }
        .status.offline {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🚀 Hello Controller Test Page</h1>
        
        <div id="status" class="status">Checking server status...</div>
        
        <!-- HelloWorldController Test -->
        <div class="test-section">
            <h2>📝 HelloWorldController Test</h2>
            <p>Test the existing HelloWorldController endpoint</p>
            <button onclick="testHelloWorld()">Test /helloworld</button>
            <div id="helloworld-result" class="result"></div>
        </div>

        <!-- HelloController Test -->
        <div class="test-section">
            <h2>👋 HelloController Test</h2>
            <p>Test the new HelloController endpoints</p>
            <button onclick="testHello()">Test /hello (default)</button>
            <div class="input-group">
                <input type="text" id="nameInput" placeholder="Enter your name" value="Alice">
                <button onclick="testHelloWithName()">Test /hello with name</button>
            </div>
            <button onclick="testWelcome()">Test /api/welcome</button>
            <div id="hello-result" class="result"></div>
        </div>

        <!-- Root Page Test -->
        <div class="test-section">
            <h2>🏠 Root Page Test</h2>
            <p>Test the root endpoint (PageController)</p>
            <button onclick="testRoot()">Test / (root page)</button>
            <div id="root-result" class="result"></div>
        </div>

        <!-- All Endpoints Test -->
        <div class="test-section">
            <h2>🔍 Test All Endpoints</h2>
            <p>Run all tests at once</p>
            <button onclick="testAll()">Test All Endpoints</button>
            <div id="all-result" class="result"></div>
        </div>
    </div>

    <script>
        // Check server status on page load
        window.onload = function() {
            checkServerStatus();
        };

        function checkServerStatus() {
            const statusDiv = document.getElementById('status');
            statusDiv.textContent = 'Checking server status...';
            statusDiv.className = 'status';

            fetch('/hello')
                .then(response => {
                    if (response.ok) {
                        statusDiv.textContent = '✅ Server is ONLINE - All endpoints available';
                        statusDiv.className = 'status online';
                    } else {
                        throw new Error('Server responded with status: ' + response.status);
                    }
                })
                .catch(error => {
                    statusDiv.textContent = '❌ Server is OFFLINE - Please start the application';
                    statusDiv.className = 'status offline';
                    console.error('Server check failed:', error);
                });
        }

        function showResult(elementId, message, isSuccess = true) {
            const element = document.getElementById(elementId);
            element.textContent = message;
            element.className = `result ${isSuccess ? 'success' : 'error'}`;
        }

        function showLoading(elementId) {
            const element = document.getElementById(elementId);
            element.textContent = 'Loading...';
            element.className = 'result loading';
        }

        async function testHelloWorld() {
            showLoading('helloworld-result');
            try {
                const response = await fetch('/helloworld');
                const text = await response.text();
                showResult('helloworld-result', `✅ Success!\nStatus: ${response.status}\nResponse: ${text}`);
            } catch (error) {
                showResult('helloworld-result', `❌ Error: ${error.message}`, false);
            }
        }

        async function testHello() {
            showLoading('hello-result');
            try {
                const response = await fetch('/hello');
                const text = await response.text();
                showResult('hello-result', `✅ Success!\nStatus: ${response.status}\nResponse: ${text}`);
            } catch (error) {
                showResult('hello-result', `❌ Error: ${error.message}`, false);
            }
        }

        async function testHelloWithName() {
            showLoading('hello-result');
            const name = document.getElementById('nameInput').value || 'World';
            try {
                const response = await fetch(`/hello?name=${encodeURIComponent(name)}`);
                const text = await response.text();
                showResult('hello-result', `✅ Success!\nStatus: ${response.status}\nName: ${name}\nResponse: ${text}`);
            } catch (error) {
                showResult('hello-result', `❌ Error: ${error.message}`, false);
            }
        }

        async function testWelcome() {
            showLoading('hello-result');
            try {
                const response = await fetch('/api/welcome');
                const text = await response.text();
                showResult('hello-result', `✅ Success!\nStatus: ${response.status}\nResponse: ${text}`);
            } catch (error) {
                showResult('hello-result', `❌ Error: ${error.message}`, false);
            }
        }

        async function testRoot() {
            showLoading('root-result');
            try {
                const response = await fetch('/');
                const text = await response.text();
                showResult('root-result', `✅ Success!\nStatus: ${response.status}\nResponse: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
            } catch (error) {
                showResult('root-result', `❌ Error: ${error.message}`, false);
            }
        }

        async function testAll() {
            showLoading('all-result');
            const results = [];
            
            try {
                // Test all endpoints
                const endpoints = [
                    { name: 'HelloWorld', url: '/helloworld' },
                    { name: 'Hello (default)', url: '/hello' },
                    { name: 'Hello (with name)', url: '/hello?name=TestUser' },
                    { name: 'Welcome', url: '/api/welcome' },
                    { name: 'Root', url: '/' }
                ];

                for (const endpoint of endpoints) {
                    try {
                        const response = await fetch(endpoint.url);
                        const text = await response.text();
                        results.push(`✅ ${endpoint.name}: ${response.status} - ${text.substring(0, 50)}${text.length > 50 ? '...' : ''}`);
                    } catch (error) {
                        results.push(`❌ ${endpoint.name}: ${error.message}`);
                    }
                }

                showResult('all-result', results.join('\n'));
            } catch (error) {
                showResult('all-result', `❌ Test failed: ${error.message}`, false);
            }
        }
    </script>
</body>
</html> 